//index.js
const app = getApp()
Page({
    data: {
        token: wx.getStorageSync('token'),
        noAuthorization: true,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        showModal: false,
        showPasswordModel: false,
        existencePassword: false,
        currentParkId: undefined,
        tel: '',
        carNumber: '',
        stationType: 1,
        station: "小型",
        cardType: 1,
        sitInfo: undefined,
        isGetParkingLot: true,
        isLogin: false,
        password: '',
        confirm_password: '',
        clickButton: false,
    },
    /**
     * 获取车位
     */
    getParkingLot: function () {
        const that = this
        let token = that.data.token
        if (!token) {
            wx.showToast({
                title: "网络延迟，请下拉刷新页面",
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (that.data.clickButton) {
            wx.showToast({
                title: "点击太快了",
                icon: 'none',
                duration: 2000
            })
            return;
        } else {
            that.setData({clickButton: true})
        }
        wx.request({
            url: app.globalData.baseurl + "sit-info/getParkingLot",
            data: {token: that.data.token},
            method: "POST",
            success: function (res) {
                if (res.data.code === 200 || res.data.code === 202) {
                    if (res.data.data) {
                        that.setData({
                            sitInfo: res.data.data,
                            isGetParkingLot: false
                        })
                    }
                } else if (res.data.code === 201) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '用户信息不完善，获取车位失败，请先完整信息',
                        showCancel: true,
                        confirmText: '确认',
                        cancelText: '取消',
                        success: (res) => {
                            if (res.confirm) {
                                that.setData({
                                    showModal: true
                                })
                            }
                        }
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 2000
                    })
                }
            },
            complete() {
                that.setData({clickButton: false})
            }
        })
    },
    onLoad: function () {

    },
    onShow: function () {
        const that = this
        let token = that.data.token
        if (!token) {
            token = wx.getStorageSync('token')
            if (token) {
                that.setData({token: token})
            }
        } else {
            that.isParkingLot()
        }
        if (app.globalData.userInfo) {
            that.getUserInfo(app.globalData.userInfo)
        }
        wx.getSetting({
            success: function (res) {
                if (!res.authSetting['scope.userInfo']) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '你未授权，将无法正常使用小程序，是否进入授权页面授权!',
                        showCancel: true,
                        confirmText: '确认',
                        cancelText: '取消',
                        success: (res) => {
                            if (res.confirm) {
                                wx.navigateTo({
                                    url: '../login/login',
                                })
                            }
                        }
                    })
                } else {
                    that.setData({
                        noAuthorization: false
                    })
                }
            }
        })
    },
    /**
     *
     */
    getUserInfo: function (userInfo) {
        let station;
        if (userInfo.stationType === 2) {
            station = '中型'
        } else if (userInfo.stationType === 3) {
            station = '大型'
        } else {
            station = '小型'
        }
        this.setData({
            cardType: userInfo.cardType,
            station: station,
        })
    },
    /**
     * 点击取消
     */
    modalCancel: function () {
        // console.log("点击取消")
    },
    /**
     * 点击确认，更新用户数据
     */
    modalConfirm: function () {
        const that = this
        if (that.data.carNumber.length === 0) {
            wx.showToast({
                title: '请输入车牌号',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (that.data.tel.length === 0) {
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (that.data.stationType.length === 0) {
            wx.showToast({
                title: '请选择车型',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        wx.request({
            url: app.globalData.baseurl + "user/updateUserByToken",
            data: {
                token: that.data.token,
                carNumber: that.data.carNumber,
                tel: that.data.tel,
                stationType: that.data.stationType,
            },
            success: function (res) {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                })
            },
            method: "POST"
        })
    },
    /**
     * 获取输入车牌号
     */
    getCarNumber: function (e) {
        this.setData({
            carNumber: e.detail.value.trim()
        })
    },
    /**
     * 获取输入手机号
     */
    getTel: function (e) {
        this.setData({
            tel: e.detail.value.trim()
        })
    },
    /**
     * 获取输入密码
     */
    getPassword: function (e) {
        this.setData({password: e.detail.value.trim()})
    },
    /**
     * 获取输入确认密码
     */
    getConfirmPassword: function (e) {
        this.setData({confirm_password: e.detail.value.trim()})
    },
    /**
     * 获取输入车型
     */
    getStationType: function (e) {
        this.setData({
            stationType: e.detail.value
        })
    },
    /**
     * 结束停车
     */
    endParkingLot: function () {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确定结束停车？',
            showCancel: true,
            confirmText: '确认',
            cancelText: '取消',
            success: (res) => {
                if (res.confirm) {
                    wx.request({
                        url: app.globalData.baseurl + "sit-info/endParkingLot",
                        data: {token: that.data.token},
                        success: function (res) {
                            const park = res.data.data
                            if (res.data.code === 200) {
                                that.setData({
                                    currentParkId: park.parkId,
                                    sitInfo: null,
                                    isGetParkingLot: true
                                })
                                wx.showModal({
                                    title: '提示',
                                    content: '费用为：' + park.fee,
                                    showCancel: true,
                                    confirmText: '支付',
                                    cancelText: '取消',
                                    success: (res) => {
                                        if (res.confirm) {
                                            that.pay()
                                            // wx.navigateTo({url: '../pay/pay?id='+park.parkId+'&fee='+park.fee})
                                        }
                                    }
                                })
                            } else {
                                wx.showToast({
                                    title: res.data.msg,
                                    icon: 'none',
                                    duration: 2000
                                })
                            }
                        },
                        method: "POST"
                    })
                }
            }
        })
    },
    /**
     * 跳转授权页面
     */
    authorization: function () {
        wx.navigateTo({
            url: '../login/login',
        })
    },
    /**
     * 判断是否停车
     */
    isParkingLot: function () {
        const that = this
        wx.request({
            url: app.globalData.baseurl + "sit-info/isParkingLot",
            data: {token: that.data.token},
            method: "POST",
            success: function (res) {
                if (res.data.code === 200) {
                    that.setData({
                        sitInfo: res.data.data,
                        isGetParkingLot: false
                    })
                } else {
                    that.setData({
                        sitInfo: null,
                        isGetParkingLot: true
                    })
                }
            },
        })
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        const that = this
        that.onShow()
        wx.request({
            url: app.globalData.baseurl + "user/getUserInfoByToken",
            method: "POST",
            data: {token: that.data.token},
            success: function (res) {
                if (res.data.code === 200) {
                    that.getUserInfo(res.data.data)
                }
                wx.stopPullDownRefresh()
            }
        })
    },
    /**
     * 支付
     */
    pay: function () {
        const that = this
        const userInfo = app.globalData.userInfo
        if (userInfo.password === 'yes') {
            that.setData({existencePassword: true})
        }
        that.setData({
            showPasswordModel: true,
            password: '',
            confirm_password: '',
        })
    },
    /**
     * 取消支付
     */
    cancelPay: function () {
        this.setData({showPasswordModel: false})
    },
    /**
     * 确认支付
     */
    confirmPay: function () {
        const that = this
        const userInfo = app.globalData.userInfo
        if (!that.data.currentParkId) {
            wx.showToast({
                title: '系统出错，请稍后重试',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (that.data.password.length === 0) {
            wx.showToast({
                title: '请输入密码',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (userInfo.password === 'no') {
            if (that.data.password !== that.data.confirm_password) {
                wx.showToast({
                    title: '两次密码不相等',
                    icon: 'none',
                    duration: 2000
                })
                return;
            }
        }
        this.setData({showPasswordModel: false})
        wx.request({
            url: app.globalData.baseurl + "user/pay",
            method: "POST",
            data: {
                token: that.data.token,
                password: that.data.password,
                parkId: that.data.currentParkId,
            },
            success: function (res) {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    /**
     * 停车收费标准
     */
    getChargerList: function () {
        wx.navigateTo({url: '../charger/charger'})
    },
})
